package com.freshcity.page.welcome
{
	import com.freshcity.component.container.Component;
	import com.freshcity.core.Dispatcher;
	import com.freshcity.core.GlobalVar;
	import com.freshcity.core.OpCodes;
	import com.freshcity.lib.event.DataEvent;
	import com.freshcity.lib.event.EventName;
	import com.freshcity.lib.tooltip.ToolTipsManager;
	import com.freshcity.manager.EffectManager;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.manager.MsgManager;
	import com.freshcity.page.common.SexIcon;
	import com.freshcity.utils.StringUtil;
	import com.freshcity.welcome.InfoTipWin;
	import com.freshcity.welcome.SelfIntroWin;
	import com.greensock.TweenLite;
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.LoaderMax;
	import com.greensock.loading.XMLLoader;
	import com.pblabs.debug.Logger;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class SelfIntro extends Component
	{
		public var data:*;
		private var back:SelfIntroWin;
		private var guidWords:XMLList;
		private var curIndex:int;
		private var sexIcon:SexIcon;

		public function SelfIntro(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			super(LayerManager.popupLayer, xpos, ypos);
			this.x=(stage.stageWidth - back.width) / 2;
			this.y=(stage.stageHeight - back.height) / 2;

			back.preBtn.visible=false;
			back.nextBtn.visible=false;
			back.submitBtn.enabled=false;
			
			back.otherDesc.mouseEnabled = false;
			back.wordsAmt.htmlText="<font color='#666666'>还可输入" + 100 + "个字</font>";
			back.selfDesc.addEventListener(Event.CHANGE, onSelfDescChangeHandler);

			back.submitBtn.addEventListener(MouseEvent.CLICK, onSubmitBtnClick);
			back.preBtn.addEventListener(MouseEvent.CLICK, onPreBtnHandlder);
			back.nextBtn.addEventListener(MouseEvent.CLICK, onNextBtnHandler);
			Dispatcher.addEventListener(EventName.LevelUp, onLevelUpHandler);

			var loader:XMLLoader=new XMLLoader(GlobalVar.MAIN_PATH + "/guidWords.xml?v=" + GlobalVar.version, {name: "guidWords", autoDispose: false, onComplete: completeHandler});
			loader.load();
		}

		private function completeHandler(event:LoaderEvent):void
		{
			var xml:XML=LoaderMax.getContent("guidWords");
			//this.guidWords=xml.word.(@sex == main.user.sex);
			this.guidWords=xml.word;
			this.curIndex=0;
			this.setGuidContent();
			Logger.debug(this, "complteHandler", "guidWords length is " + this.guidWords.length());
		}

		private function setGuidContent():void
		{
			back.otherDesc.htmlText=StringUtil.trim(this.guidWords[this.curIndex].text());
			var sex:String=this.guidWords[this.curIndex].@sex;
			sexIcon.data = sex;
			back.userName.text = '用户' + this.guidWords[this.curIndex].@id;
			back.preBtn.visible=(curIndex > 0) ? true : false;
			back.nextBtn.visible=(curIndex < (this.guidWords.length() - 1)) ? true : false;
		}

		private function onLevelUpHandler(evt:DataEvent):void
		{
			if(this.main.user.qqNo == ''){
				this.main.settingCallback = this.main.registerQQNo;
				var setting:FriendCond = new FriendCond();
				setting.main = this.main;
				this.destroy();
				setting.show();
			}
			
			//main.msg.showPopUpMsg('使用中有困扰，请点击上方“帮助”链接获得支持！',MsgManager.HAHA,0,MsgManager.BOTTOM_RIGHT);
			/*
			var win:InfoTipWin=new InfoTipWin();
			LayerManager.popupLayer.addChild(win);
			win.x=(win.stage.stageWidth - win.width) / 2;
			win.y=(win.stage.stageHeight - win.height) / 2;
			
			TweenLite.from(win,0.8,{alpha:0,y:"-30",delay:1});

			var myfun:Function=function(event:Event):void
			{
				LayerManager.popupLayer.removeChild(win);
			};

			win.submitBtn.addEventListener(MouseEvent.CLICK, myfun);
			*/
		}

		protected function onNextBtnHandler(event:MouseEvent):void
		{
			this.curIndex++;
			this.setGuidContent();
		}

		protected function onPreBtnHandlder(event:MouseEvent):void
		{
			this.curIndex--;
			this.setGuidContent();
		}

		override protected function addChildren():void
		{
			super.addChildren();
			back=new SelfIntroWin();
			addChild(back);
			
			sexIcon = new SexIcon(back,286,360);
		}

		override public function destroy():void
		{
			super.destroy();
			Dispatcher.removeEventListener(EventName.LevelUp, onLevelUpHandler);
			back.submitBtn.removeEventListener(MouseEvent.CLICK, onSubmitBtnClick);
			back.preBtn.removeEventListener(MouseEvent.CLICK, onPreBtnHandlder);
			back.nextBtn.removeEventListener(MouseEvent.CLICK, onNextBtnHandler);
			if(EffectManager.hasUIMask())EffectManager.hideUIMask();
		}

		override public function show(event:Event=null):void
		{
			if(!EffectManager.hasUIMask())EffectManager.addUIMask([]);
			if(main.user.selfDesc.length>0){
				back.selfDesc.text = main.user.selfDesc;
			}
			super.show(event);
		}

		private function onSelfDescChangeHandler(event:Event):void
		{
			var c1:String=StringUtil.trim(back.selfDesc.text);
			var loc1:int=StringUtil.getANSILength(c1);
			if (loc1 > 0 && loc1 <= 200)
			{
				var i:int = (200 - loc1) / 2;
				back.wordsAmt.htmlText="<font color='#666666'>还可输入" + i + "个字</font>";
				back.submitBtn.enabled=true;
			}
			else if (loc1 > 200)
			{
				var j:int = (loc1 - 200) / 2;
				back.wordsAmt.htmlText="<font color='#FF3603'>超出" + j + "个字</font>";
				back.submitBtn.enabled=false;
			}
			else
			{
				back.submitBtn.enabled=false;
			}
		}

		private function onSubmitBtnClick(event:MouseEvent):void
		{
			var len:int=StringUtil.getANSILength(back.selfDesc.text);
			if (len <= 5)
			{
				main.msg.showRollTipsMsg("自我描述太简单啦");
				return;
			}
			sendServer();
		}

		private function sendServer():void
		{
			var value1:String=StringUtil.trim(back.selfDesc.text);
			back.submitBtn.enabled=false;
			this.data.desc = value1;
			main.commu.send(OpCodes.UPGRADE_LEVEL, data);
		}
	}
}
